package com.time.timespace.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.time.timespace.domain.entity.Category;
import com.time.timespace.mapper.CategoryMapper;
import com.time.timespace.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private final CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void deleteCategory(Long id) {
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId, id);
        categoryMapper.delete(wrapper);
    }

    @Override
    public Category saveCategory(Category category) {
        if (Objects.isNull(category.getId())) {
            // 新增
            categoryMapper.insert(category);
        } else {
            // 更新
            categoryMapper.updateById(category);
        }
        Long id = category.getId();
        return categoryMapper.selectById(id);
    }

    @Override
    public List<Category> selectAll(Long userId) {
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getUserId, userId);
        return categoryMapper.selectList(wrapper);
    }

}
